package cn.com.captureNetPacket.marge;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.Mixer.Info;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;

import org.springframework.util.ResourceUtils;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Data
// 测试soundapi合成的，感觉没什么用，需要硬件设备，多线程的时候担心出问题。暂时不用
public class testSoundApiFile {
	public File file;
	File outputFile = new File("recoder.wav");
	AudioFormat audioFormat = new AudioFormat(
	        AudioFormat.Encoding.PCM_SIGNED, 44100.0F, 16, 2, 4, 44100.0F,
	        false);
			public testSoundApiFile(String fileName) throws FileNotFoundException {
				file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX+fileName);
			}
				public void sourceWriteToFile(SourceDataLine line) {
					AudioInputStream cin;
					try {
						cin = AudioSystem.getAudioInputStream(file);
						AudioFormat format = cin.getFormat();
						DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
						  line = (SourceDataLine) AudioSystem.getLine(info);
						   
						line.open(format);//或者line.open();format参数可有可无
						line.start();
						int nBytesRead = 0;
						byte[] buffer = new byte[512];
						while (true) {
						    nBytesRead = cin.read(buffer, 0, buffer.length);
						    if (nBytesRead <= 0)
						        break;
						    line.write(buffer, 0, nBytesRead);
						}
						line.drain();
						line.close();
					} catch (UnsupportedAudioFileException e) {
						log.error("UnsupportedAudioFileException",e);
					} catch (IOException e) {
						log.error("IOException",e);
					} catch (LineUnavailableException e) {
						log.error("LineUnavailableException",e);
					}
				 
				}
				
				
				
				
				
				
				public void getFileInfo() {
					try {
						log.info("---------AudioFileFormat---------|||"+file.getPath());
						AudioFileFormat aff = AudioSystem.getAudioFileFormat(file);
						AudioInputStream ais = AudioSystem.getAudioInputStream(file);
						//AudioFormat既可以通过AudioInputStream获取,也可以通过AudioFileFormat获取.
						AudioFormat af = ais.getFormat();// aff.getFormat()
						log.info("---------AudioFileFormat---------");
						log.info("Type " + aff.getType());
						log.info("byteLength " + aff.getByteLength());
						log.info("frame length " + aff.getFrameLength());
						log.info("format " + aff.getFormat());
						log.info("properties " + aff.properties());
						log.info("--------AudioFormat----------");
						log.info("encoding " + af.getEncoding());
						log.info("channels " + af.getChannels());
						log.info("sampleRate " + af.getSampleRate());
						log.info("frameRate " + af.getFrameRate());
						log.info("properties " + af.properties());
						log.info("sampleSizeInBits " + af.getSampleSizeInBits());
						log.info("frameSize " + af.getFrameSize());
						log.info("--------------AudioInputStream-------");
						log.info("frameLength " + ais.getFrameLength());
//						播放时长=frameLength/frameRate
//						frameRate=sampleRate
//						frameSize=channels*sampleSizeInBytes=channels*sampleSizeInBits/8
//						AudioFileFormat.getByteLength=文件头长度+数据长度=文件头长度+frameSize*frameLength
						//以上结论完全适用于wav文件,但不适用于mp3文件,因为mp3文件是经过压缩的,并不是原始音频数据.
					} catch (UnsupportedAudioFileException e) {
						log.error("UnsupportedAudioFileException",e);
					} catch (IOException e) {
						log.error("IOException",e);
					}
				}
				
				  public static void listMixers() {
				        Mixer.Info[] a = AudioSystem.getMixerInfo();
				        log.info("查看系统中的全部混音器");
				        for (int i = 0; i < a.length; i++) {
				        	 Mixer mixer = AudioSystem.getMixer(a[i]);
				        	 Line.Info[] b = {new Line.Info(SourceDataLine.class),
				                     new Line.Info(TargetDataLine.class),
				                     new Line.Info(Clip.class)};
				             int ans = 0;String isSupport="";
				             for (int j = 0; j < b.length; j++) {
				                 if (mixer.isLineSupported(b[j])) {
				                     ans |= (1 << j);
				                     isSupport=isSupport+" 支持"+b[j].getLineClass()+"线路,";
				                 }
				             }
				             log.info(" 编号:"+i+" name: "+a[i].getName()+" version: "+a[i].getVersion()+" 供应商: "+a[i].getVendor()+" 描述: "+a[i].getDescription()+" ans:"+ans+" "+isSupport);
				        }
				        log.info("查看系统中的全部混音器。");
				    }
				  public static Info getMixerInfo(int i) {
					  Mixer.Info[] a = AudioSystem.getMixerInfo();
						  log.info("getMixerInfo name: "+a[i].getName()+" version: "+a[i].getVersion()+" 供应商: "+a[i].getVendor()+" 描述: "+a[i].getDescription());
					  return a[i];
				  }
				
				
				//查看支持的文件类型
				public static void listSupportedTypes() {
			        AudioFileFormat.Type[] aTypes = AudioSystem.getAudioFileTypes();
			        log.info("本系统支持的文件类型:");
			        for (int i = 0; i < aTypes.length; i++) {
			            log.info(aTypes[i].getExtension());
			        }
			        log.info("本系统支持的文件类型。");
			    }
}
